package com.RequestGongWuShu;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.requestGwsLongGangQu.Elevator;
import com.requestGwsLongGangQu.GwsLgqUtil;
import com.yishun.springboot.dao.CWRServiceTest;
import com.yishun.springboot.pojo.RequestGongWuShuTspBaseInfo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TspDistrictlevel {

	private String api_key = "aa4b15df56e04bd3a481f1a64fc43656";
	private String api_version = "1.0";
	private String engCode = "XM20230098-1";// 工程编号
	private String projectCode = "XM20230098";// 项目编号
	private String baseUlr = "http://218.17.147.31:66";// 请求地址

	public String addBasicInfo(String apiKey, String projectCode, String engCode, Object r) {
		String url = baseUlr + "/openapi/envMonitoring/addBasicInfo";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		String body = JSON.toJSONString(r);
		JSONObject json = JSONObject.parseObject(body);

		String signature = getSignature(apiKey, api_version, engCode, projectCode, timestamp, body);
		log.info("sha:" + signature);

		String a = GwsLgqUtil.GwsParamUtil(json, timestamp, signature, api_key, projectCode, engCode, api_version);

		log.info("最终请求信息：" + a);

		String info = HttpUtil.post(url, a);

		log.info("down!" + info);

		return info;
	}

	public String addRecord(String apiKey, String projectCode, String engCode, Object r) {

		String url = baseUlr + "/openapi/envMonitoring/addRecord";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
		JSONObject param = new JSONObject();
		param.put("api_key", apiKey);
		param.put("project_code", projectCode);
		param.put("eng_code", engCode);
		param.put("api_version", api_version);
		param.put("timestamp", timestamp);

		String j2r = JSON.toJSONString(r);
		JSONObject json = JSONObject.parseObject(j2r);

		List<JSONObject> bodyList = new ArrayList<>();// 可以传入多个body
		bodyList.add(json);
		param.put("body", bodyList);// body
		String jsonBody = JSONObject.toJSONString(bodyList);// body的json字符串
		log.info("body:" + jsonBody);
		String signature = getSignature(apiKey, api_version, engCode, projectCode, timestamp, jsonBody);
		param.put("signature", signature);
//        发送请求
		String info = HttpUtil.post(url, param.toJSONString());

		log.info("最终结果提交：" + param.toJSONString());

		log.info("区工务署返回值：" + info);

		return info;

	}

	public static String getSignature(String apiKey, String apiVersion, String engCode, String projectCode,
			String timestamp, String body) {
		String str4MD5 = String.format("api_key%sapi_version%sbody%seng_code=%sproject_code=%stimestamp%s", apiKey,
				apiVersion, body.isEmpty() ? "{}" : body, engCode, projectCode, timestamp);
		return getMD5Code(str4MD5);
	}

	public static String getMD5Code(String strObj) {
		String resultString = null;
		try {
			resultString = new String(strObj);
			MessageDigest md = MessageDigest.getInstance("MD5");
			Charset charset = Charset.forName("UTF-8");
			resultString = byteToString(md.digest(strObj.getBytes(charset)));
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return resultString;
	}

	// 转换字节数组为16进制字串
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}

	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}

	// 全局数组
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
			"E", "F" };
}
